﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class StarSystem : MonoBehaviour {

	public int Recource;
	public int RecourceUse;
	public int Owner;
    GameObject StarWindow;
    public enum States
	{
		origin,
		colonized,
		destroyed
	}
	public States State_;

	[Serializable]
	public class Visible
	{
		public enum VisibleTypes
		{
			Owend,
			Unexplore,
			Explored,
			Uncomunicate,
			enemy
		}
		public VisibleTypes VisibleType;
		public float ExplorCD;
		public float ComunicateCD;
	}
	[SerializeField][HideInInspector]
	public List<Visible> VisibleState;

	void CDSet(){
		//过一段时间自动变为未探索
		for (int j = 0; j < VisibleState.Count; j++) {
			if (VisibleState[j].ExplorCD < 0 && State_ != States.destroyed) {
				VisibleState[j].ExplorCD = 0;
				VisibleState [j].VisibleType = Visible.VisibleTypes.Unexplore;
			}
			else if (VisibleState[j].ExplorCD < 0)
			{
				VisibleState[j].ExplorCD -= UnityEngine.Time.deltaTime;
			}
			if (VisibleState[j].ComunicateCD < 0) {
				VisibleState[j].ComunicateCD = 0;
				if(VisibleState [j].VisibleType != Visible.VisibleTypes.Uncomunicate)
					ControlCenter.Instance.Players [Owner-1].GetComponent<Player> ().CurState.EnemySystemCount += 1;
				VisibleState [j].VisibleType = Visible.VisibleTypes.enemy;
			}
			else if (VisibleState[j].ComunicateCD < 0)
			{
				VisibleState[j].ComunicateCD -= UnityEngine.Time.deltaTime;
			}
		}
	}
	float Rate;
	void Update () {
		if (Rate < 0) {
			Rate = ControlCenter.Instance.YearRate;
			if (State_ == States.colonized && Owner!=0) {
				if (Recource > 0) {
					Recource -= RecourceUse;
					GameObject Owner_ = OwnerPlayer ();
					//Owner_.GetComponent<Player> ().CurState.ResourceInStore += RecourceUse;
                    GameManager.Instance.ChangeResource(Owner_, -RecourceUse);
				} 
				else 
				{
					Recource = 0;
					GameObject Owner_ = OwnerPlayer ();
					//Owner_.GetComponent<Player> ().CurState.ResourceInStore -= RecourceUse/4;
                    GameManager.Instance.ChangeResource(Owner_, RecourceUse/4);
                }
			}
		} 
		else {
			Rate -= UnityEngine.Time.deltaTime;
		}
			
		//if (Recource < 0) //资源耗尽
		//{
			//State_ = States.destroyed;
			//ControlCenter.Instance.Players [Owner-1].GetComponent<Player> ().CurState.OwnedSystems -= 1;
			//Owner = 0;
		//}

		CDSet ();
		TextSet ();
		ColorSet ();
	}

	void Start () {

        StarWindow = GameObject.Find("Canvas").transform.Find("StatPanel").gameObject;

        for (int i=0;i<ControlCenter.Instance.Players.Count;i++)//预置
		{
			Visible vis = new Visible();
			vis.VisibleType = Visible.VisibleTypes.Unexplore;
			VisibleState.Add (vis);
		}
		if (Owner != 0) {
			State_ = States.colonized;
			VisibleState[Owner-1].VisibleType = Visible.VisibleTypes.Owend;
		}
			
	}

	public GameObject OwnerPlayer()
	{
		GameObject[] Players;
		Players = GameObject.FindGameObjectsWithTag ("player");
		foreach (GameObject play in Players) {

			if (play.GetComponent<Player> ().PlayerID == Owner)
				return play;
		}
		return null;
	}

	TextMesh Owner_txt;
	TextMesh Resource_txt;
	float RecourceTotal;
	void Awake(){
		Owner_txt=transform.Find ("Owner").GetComponent<TextMesh> ();
		Owner_txt.gameObject.transform.position = new Vector3 (-2,2,0)/5 ;
		Owner_txt.fontSize = 125;
		Owner_txt.color = new Color (0,0.9f,1f);
		Resource_txt=transform.Find ("Resource").GetComponent<TextMesh> ();
		Resource_txt.gameObject.transform.position = new Vector3 (0,-2,0)/5;
		Resource_txt.fontSize = 55;
		Resource_txt.color = new Color (0.3f,0.8f,1f);

		RecourceTotal = Recource;
	}
	void TextSet(){
		Owner_txt.text = Owner.ToString ();
		if (Owner == 0)
			Owner_txt.text = "";
		
		if (State_ == States.colonized) {
			if (Recource > 0) {
				Resource_txt.text = Recource.ToString () + "/" + RecourceTotal.ToString () + "-" + RecourceUse.ToString ();
				Resource_txt.color = new Color (0.3f,0.8f,1f);
			} else {
				Resource_txt.text =  "-" + (RecourceUse/4).ToString ();
				Resource_txt.color = new Color (0.8f,0.2f,1f);
			}

		}
		else
			Resource_txt.text = "";
	}
	void ColorSet(){
		switch (State_) {
		case States.colonized:
			if(Recource>0)
				GetComponent<SpriteRenderer> ().color = new Color (1,1,1);
			else
				GetComponent<SpriteRenderer> ().color = new Color (0.8f,0.8f,0.8f);
			break;
		case States.origin:
			GetComponent<SpriteRenderer> ().color = new Color (0.5f,0.5f,0.8f);
			break;
		case States.destroyed:
			GetComponent<SpriteRenderer> ().color = new Color (0.75f,0.25f,0.25f);
			break;
		default:
			break;
		}
	}
  
}
